/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sampleServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import sampleBean.CusSessionBeanRemote;

/**
 *
 * @author ASUS K43E
 */
public class Admin extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Context context = new InitialContext();
            Object obj = context.lookup("remoteJNDI");
            CusSessionBeanRemote remote = (CusSessionBeanRemote) obj;
            String button = request.getParameter("btAction");
            HttpSession session = request.getSession();

            //search customer
            if (button.equals("Search")) {
                String name = request.getParameter("txtSearch");
                List list = remote.searchCus(name);
                request.setAttribute("CUSLIST", list);
                RequestDispatcher rd = request.getRequestDispatcher("admin.jsp");
                rd.forward(request, response);
            } //block and lost
            else if (button.equals("Block") || button.equals("UnBlock") || button.equals("Lost") || button.equals("NotLost")) {
                String username = request.getParameter("txtUsername");
                String name = request.getParameter("txtSearch");
                boolean block = false;
                boolean lost = false;
                if (button.equals("Block")) {
                    block = true;
                } else if (button.equals("UnBlock")) {
                    block = false;
                } else if (button.equals("Lost")) {
                    block = true;
                    lost = true;
                } else if (button.equals("NotLost")) {
                    lost = false;
                    block = true;
                }
                boolean result = remote.blockAndLost(username, block, lost);
                String url = "";
                if (result) {
                    url = "Admin?btAction=Search&txtSearch=" + name;
                }
                response.sendRedirect(url);
            }//pending
            else if (button.equals("Pending")) {
                List list = remote.checkNew();
                request.setAttribute("PENDING", list);
                RequestDispatcher rd = request.getRequestDispatcher("admin.jsp");
                rd.forward(request, response);
            }//update card info
            else if (button.equals("SaveSearch")) {
                String username = request.getParameter("txtUsername");
                String cardId = request.getParameter("txtCardId");
                int point = Integer.parseInt(request.getParameter("txtPoint"));
                String rank = request.getParameter("ddlRank");
                String blockStr = request.getParameter("cbBlock");
                String name = request.getParameter("txtSearch");
                boolean block = false;
                if (blockStr != null) {
                    block = true;
                }
                String lostStr = request.getParameter("cbLost");
                boolean lost = false;
                if (lostStr != null) {
                    lost = true;
                    block = true;
                }
                boolean result = remote.updateCard(username, cardId, point, rank, block, lost);
                String url = "";
                if (result) {
                    url = "Admin?btAction=Search&txtSearch=" + name;
                }
                response.sendRedirect(url);
            }//save on report page
            else if (button.equals("SaveReport")) {
                String username = request.getParameter("txtUsername");
                String cardId = request.getParameter("txtCardId");
                int point = Integer.parseInt(request.getParameter("txtPoint"));
                String rank = request.getParameter("ddlRank");
                String blockStr = request.getParameter("cbBlock");
                boolean block = false;
                if (blockStr != null) {
                    block = true;
                }
                String lostStr = request.getParameter("cbLost");
                boolean lost = false;
                if (lostStr != null) {
                    lost = true;
                    block = true;
                }
                boolean result = remote.updateCard(username, cardId, point, rank, block, lost);
                String url = "";
                if (result) {
                    url = "admin.jsp";
                }
                response.sendRedirect(url);
            }//report
            else if (button.equals("findLost") || button.equals("findBlock") || button.equals("Regular") || button.equals("Silver") || button.equals("Gold")) {
                List list = null;
                if (button.equals("findLost")) {
                    list = remote.findLost();
                } else if (button.equals("findBlock")) {
                    list = remote.findBlock();
                } else if (button.equals("Regular") || button.equals("Silver") || button.equals("Gold")) {
                    list = remote.findbyRank(button);
                }
                request.setAttribute("REPORT", list);
                RequestDispatcher rd = request.getRequestDispatcher("admin.jsp");
                rd.forward(request, response);
            }//approve
            else if (button.equals("Approve")) {
                String username = request.getParameter("txtUsername");
                boolean result = remote.approve(username);
                String url = "";
                if (result) {
                url = "Admin?btAction=Pending&txtSearch=";
                }
                response.sendRedirect(url);
            }//disapprove
            else if (button.equals("Disapprove")) {
                String username = request.getParameter("txtUsername");
                boolean result = remote.disapprove(username);
                String url = "";
                if (result) {
                url = "Admin?btAction=Pending&txtSearch=";
                }
                response.sendRedirect(url);
            }
        } catch (NamingException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
